package com.nciae.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nciae.po.ProductCategory;
import com.nciae.dao.ProductCategoryMapper;
import com.nciae.service.IProductCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author YangChen
 * @since 2021-10-14
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements IProductCategoryService {


    @Override
    public List<ProductCategory> findByWapper(ProductCategory productCategory) {
        return null;
    }

    @Override
    public List<ProductCategory> findByCategoryType(List<Integer> categoryTypes) {
        List<ProductCategory> productCategories = new ArrayList<ProductCategory>();
        Iterator<Integer> iterator = categoryTypes.iterator();
        while (iterator.hasNext()) {
            Integer cateGoryType = iterator.next();
            QueryWrapper<ProductCategory> wrapper = new QueryWrapper<ProductCategory>();
            wrapper.eq("category_type", cateGoryType);
            List<ProductCategory> products = baseMapper.selectList(wrapper);
            Iterator<ProductCategory> productsIterator = products.iterator();
            while (productsIterator.hasNext()) {
                productCategories.add(productsIterator.next());
            }
        }
        return productCategories;
    }

    @Override
    public ProductCategory findByCategoryType(Integer catagoryType) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("category_type",catagoryType);
        ProductCategory productCategory = baseMapper.selectOne(wrapper);
        return productCategory;
    }

    @Override
    public List<ProductCategory> insert(ProductCategory productCategory) {
        baseMapper.insert(productCategory);
        return findAll();
    }

    @Override
    public List<ProductCategory> update(ProductCategory productCategory) {
        baseMapper.updateById(productCategory);
        return findAll();
    }

    @Override
    public List<ProductCategory> delete(Integer id) {
        baseMapper.deleteById(id);
        return findAll();
    }

    @Override
    public ProductCategory findOne(Integer categoryId) {
        return baseMapper.selectById(categoryId);
    }

    @Override
    public List<ProductCategory> findAll() {
        return baseMapper.selectList(null);
    }

    @Override
    public List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList) {
        return null;
    }
}
